package me.shurufa.activities;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.shurufa.R;
import me.shurufa.utils.Constants;

/**
 * BaseActivity基类
 *
 * @FileName: me.shurufa.activities.BaseActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-14 15:40
 */
public class BaseActivity extends AppCompatActivity {
  protected int activity_layout_res;

  @Bind(R.id.toolbar)
  @Nullable
  LinearLayout toolbar;

  @Bind(R.id.toolbar_title)
  @Nullable
  TextView titleTextView;

  @Bind(R.id.back)
  @Nullable
  View backInToolbar;

  View rootView;
  public SwipeBackLayout mSwipeBackLayout;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置横向滑动
    // mSwipeBackLayout = getSwipeBackLayout();
    // mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
    setTheme(R.style.AppTheme);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    if (activity_layout_res != 0)
      setContentView(activity_layout_res);
    ButterKnife.bind(this);
    EventBus.getDefault().register(this);
    if (backInToolbar != null)
      backInToolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          InputMethodManager inputMethodManager = (InputMethodManager)BaseActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputMethodManager.hideSoftInputFromWindow(v.getWindowToken() , 0);
          onBackPressed();
        }
      });
    rootView = getWindow().getDecorView().findViewById(android.R.id.content);
  }

  @Override
  protected void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
  }

  @Override
  protected void onPause() {
    super.onPause();
    MobclickAgent.onPause(this);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
  }

  public void onEventMainThread(Intent intent) {
    if (intent.getAction().equals(Constants.ACTION_USER_CHANGE)){
        if (!(this instanceof LoginActivity)||!(this instanceof SettingsActivity)){
          finish();
        }
    }
  }
}
